using System;
using System.ComponentModel;

namespace Pdf2epub.Model
{
    public class HtmlReplacementTypeConverter : ExpandableObjectConverter// TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return ((sourceType == typeof(string)) || (sourceType == typeof(HtmlReplacement)));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return ((destinationType == typeof(string)) || (destinationType == typeof(HtmlReplacement)));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value != null && value.GetType() == typeof(HtmlReplacement))
                return Enum.Parse(typeof(HtmlReplacement), value.ToString());
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
                return value.ToString();
            if (destinationType == typeof(HtmlReplacement))
                return Enum.Parse(typeof(HtmlReplacement), value.ToString());
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}